<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>跑马灯</title>
</head>
<body>

    <div id="app">
        <button @click='goStart'>开始</button>
        <button @click="goEnd">结束</button>
        <p>{{msg}}</p>
    </div>

    <script src="./lib/vue.min.js"></script>
    <script>
        let vm = new Vue({
            el: '#app',
            data: {
                msg: '学习是一种信仰',
                timer: null
            },
            methods: {
                goStart(){
                    if (this.timer) return //方式一
                    // if (this.timer) clearInterval(this.timer) //方式二
                   this.timer = setInterval(()=> {
                        let startChart = this.msg.substring(0,1) // 被截掉的字符串
                        let endChart = this.msg.substring(1) // 截掉第一个后的字符串
                        this.msg =endChart +  startChart
                        console.log(this.msg)
                    },2000)
                },
                goEnd() {
                    clearInterval(this.timer)
                    this.timer = null // 如果使用方式一，执行该函数时，需要清空this.timer
                }
            }
            
        })
    </script>
</body>
</html>
